In [1]:
import numpy as np
import pandas as pd
In [2]:
series = pd.Series(20)
series
In [3]:
series[0]
In [4]:
series2 = pd.Series(range(1, 11))
series2
In [5]:
print(series2.values)
print(series2.index)
In [6]:
series3 = pd.Series(range(1, 5), index=['a', 'b', 'c', 'd'])
print(series3)
print('-'*50)
print(series3.values)
print('-'*50)
print(series3.index)
In [7]:
series3
Out[7]:
In [8]:
print(series3[1])
print(series3['b'])
In [9]:
series3[['a', 'b']]
Out[9]:
In [10]:
print(series3.loc['a'])
print(series3.iloc[0])
In [11]:
print(series3.loc[['a', 'b']])
print(series3.iloc[[0, 1]])
In [12]:
series4 = pd.Series(1, index=series3.index)
series4
Out[12]:
In [13]:
series5 = pd.Series(np.random.randn(5))
series5
Out[13]:
In [14]:
series6 = pd.Series({'math':100, 'sci':80})
series6
Out[14]:
In [15]:
s = pd.Series([2, 1, 2, 3, np.nan])
s
Out[15]:
In [16]:
print(len(s))
print(s.size)
In [17]:
# return tuple
s.shape
Out[17]:
In [18]:
# counting
s.count()
Out[18]:
In [19]:
# except duplicates
s.unique()
Out[19]:
In [20]:
# count values except NaN
s.value_counts()
Out[20]:
In [21]:
# top 5 elements
s.head()
Out[21]:
In [22]:
# low 5 elements
s.tail()
Out[22]:
In [23]:
s.head(n=2)
Out[23]:
In [24]:
# take by 0-based index
s.take([0, 1])
Out[24]:
In [25]:
s = pd.Series(range(1, 3), index=['a', 'b'])
s.take(['a', 'b'])
In [1]:
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([4, 3, 2, 1], index=['d', 'c', 'b', 'a'])
print(s1)
print(s2)
In [2]:
s1 + s2
Out[2]:
In [3]:
# element-wise in numpy
a1 = np.array([1, 2, 3, 4])
a2 = np.array([4, 3, 2, 1])
a1 + a2
Out[3]:
In [4]:
s1 * s2
Out[4]:
In [5]:
s1 ** 3
Out[5]:
In [6]:
s3 = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'f'])
s4 = pd.Series([4, 3, 2, 1, 0], index=['d', 'c', 'b', 'a', 'g'])
s3 + s4
Out[6]:
In [7]:
np_array = np.array([1, 2, 3, np.NaN])
pd_series = pd.Series([1, 2, 3, np.NaN])
In [8]:
np_array.mean()
Out[8]:
In [9]:
pd_series.mean()
Out[9]:
In [10]:
pd_series.mean(skipna=False)
Out[10]: